/***
 *  @description
 *  @author z
 *  @date: 2020/8/11
 *  @version: v1.0
 */

import cn.hutool.core.io.file.FileAppender;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class CodeLineCounter
{


	private static int totalLine;

//	private static String fileToBeCounted[] = {"java", "jsp", "css","html", "htm","xml"};//所需计算的源码文件类型
	private static String fileToBeCounted[] = {"vue","html"};//所需计算的源码文件类型
	// 整合代码到一个文件
	private static FileAppender appender =null;

	// 校验文件类型是否计算在内
	private static boolean isFileToBeCounted(String type)
	{
		for (int i = 0; i < fileToBeCounted.length; i++)
		{
			if (type.equals(fileToBeCounted[i]))
				return true;
		}
		return false;
	}

	// 获取文件类型
	private static String getType(String filename)
	{
		//System.out.println(filename);
		byte b[] = filename.getBytes();
		byte[] type = new byte[10];
		String rts = null;
		int i = 0, p = 0, n = filename.length();
		for (i = 0; i < n; i++)
		{
			if (b[i] == '.')
			{
				p = i;
				break;
			}
		}
		//System.out.print(p);
		i = p + 1;
		p = 0;
		for (; i < n && p < 10; i++)
		{
			type[p++] = b[i];
			//System.out.print(b[i]);
		}
		rts = new String(type);
		//System.out.println(rts.substring(0, p));
		return rts.substring(0, p);
	}

	// 计算文件行数
	private static void countLine(String path)
	{
		File file = new File(path);
		File lists[] = file.listFiles();
		if(null==lists){
			return;
		}
		for (int i = 0; i < lists.length; i++)
		{
			if (lists[i].isFile())
			{
				String filename = lists[i].getName();
				boolean isFileToBeCounted = isFileToBeCounted(getType(filename));
				if (isFileToBeCounted)
				{
					try
					{
						int lines = 0;
						FileReader read = new FileReader(path + filename);
						BufferedReader br = new BufferedReader(read);
						String row;
						while ((row = br.readLine()) != null)
						{
							// 将每行的数据row追加到文件中
							if(totalLine<=2000){
								if(row.trim()==""){
									continue;
								}
								appender.append(row);
								appender.flush();
							}
							lines++;
						}
						System.out.println("文件：" + path + filename + "共:" + lines + "行代码;");
						totalLine += lines;
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			} else
			{
				String paths = path;
				paths = paths + lists[i].getName() + File.separator;
				System.out.println("进入目录：" + paths + ";");
				countLine(paths);
			}
		}
	}

	public static void main(String[] args)
	{
		totalLine = 0;

//		String path ="/Users/zhao/workspace/zhaoGit/javaProject/RuoYi-Vue/ambulance/src/main/";//项目的绝对地址
		// web_doctor
		//pad_end
		//large_video_web
		//android_end
		//ambulance-ui
		String path ="/Users/zhao/temp/5g_web/android_end/";//项目的绝对地址
		File targetFile = new File("/Users/zhao/temp/android_end.txt");
		appender = new FileAppender(targetFile, 1024, true);

		countLine(path);
		System.out.println("整个项目共:" + totalLine + "行代码;");
	}

	@Test
	public void test()
	{


		File file = new File("/Users/zhao/temp/code2.txt");
		FileAppender appender = new FileAppender(file, 16, true);
		appender.append("123");
		appender.append("abc");
		appender.append("xyz");

		appender.flush();
		appender.toString();

	}

}